﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ymodem_HostMachine.Ymodem_Protocols;


namespace Ymodem_HostMachine
{
    public partial class MainForm : Form
    {   
     

       

        public MainForm()
        {
            InitializeComponent();      //初始化控件窗口   
        }

        private void Form1_Load(object sender, EventArgs e)
        {
   
            BaduRateCbb.SelectedIndex = 0;
            TransportCbb.SelectedIndex=0;
        }


        /*  升级固件文件路径*/
        private string __LoadUpdataFirmwareFilePath = null;

        /* 升级文件加载状态*/
        private bool __LoadUpdataFileReadyStatus = false;

/*  
    加载升级文件
 
 */
        private void startIAPBtn_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Multiselect = false; // 默认就是 false
                ofd.Title = "请选择文件";
                ofd.Filter = "二进制文件 (*.bin)|*.bin";

                if (ofd.ShowDialog() == DialogResult.OK)//判断是选中文件
                {
                    FileInfo f_upFIleInfo = new FileInfo(ofd.FileName); //创建FileInfo对象


                    // 1. 读文件到 byte[]
                    byte[] fileBytes = File.ReadAllBytes(ofd.FileName);

                    // 2. 计算 CRC
                    ushort crc = CRC16.getCRC16(fileBytes, fileBytes.Length);
               
                    ClearUpdateBinFileDisplay();
                    AddUpdateBinFileDisplay("文件名         :" + ofd.SafeFileName + "\r\n"); //文件名(即包含绝对路径的文件名)
                    AddUpdateBinFileDisplay("文件长度       :" + f_upFIleInfo.Length.ToString() + " 字节\r\n"); //文件字节大小
                    AddUpdateBinFileDisplay("创建日期       :" + f_upFIleInfo.CreationTime.ToString() + "\r\n"); //文件创建日期
                    AddUpdateBinFileDisplay("最后修改时间   :" + f_upFIleInfo.LastWriteTime.ToString() + "\r\n"); //文件最后次修改日期
                    AddUpdateBinFileDisplay("CRC16          :" + "0x"+crc.ToString("X4") + "\r\n");               //文件CRC16数值

                    __LoadUpdataFileReadyStatus = true;
                    __LoadUpdataFirmwareFilePath = ofd.FileName;
                }
                else
                {
                    AddUpdateBinFileDisplay("添加BIN文件出错");
                    __LoadUpdataFileReadyStatus = false;
                }
            }
        }

        private void AddUpdateBinFileDisplay(string info)
        {
            this.Invoke(new Action(() =>
            {
                DebugInfo_richBox.AppendText(info);
            }));
        }
        //文件信息显示窗口清空显示
        private void ClearUpdateBinFileDisplay()
        {
            this.Invoke(new Action(() =>
            {
                DebugInfo_richBox.Text = "";
            }));
        }

      

/*调试信息文本框清空*/
        private void ProcessInfoClear()
        {
            this.Invoke(new Action(() =>
            {
                DebugInfo_richBox.Clear();
            }));
        }

        private void DebugInfo_richBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
